<?php
ob_start();
header("Content-type:text/html;charset=utf-8");
ini_set('memory_limit','88M');
ini_set('max_execution_time','0');
date_default_timezone_set('Asia/Shanghai');
error_reporting(0);
include 'config.php';

/*----------链接mysql数据库  start------------*/
$conn = @mysql_connect($db_host,$db_user,$db_pwd) or die('mysql server failed!!');
@mysql_query('set names utf8',$conn);
@mysql_select_db($db_name,$conn) or die('can not find mysql dbname:'.$db_name);
/*----------链接mysql数据库  end------------*/

/*----------常用方法封装  start------------*/
function querys($sql){
	return @mysql_query($sql);
}

function getall($sql){
	$query = querys($sql);
	$list = '';
	while($row = @mysql_fetch_assoc($query)){
		$list[] = $row;
	}
	return $list;
}

function getone($sql){
	$query = querys($sql);
	return @mysql_fetch_assoc($query);
}
/*
*修改实例
*$table=表名
*$err=数组
*$id=编号
*/
function edit_update_cl($table,$err,$id){
	$sql = "UPDATE ".$table." SET ";
	foreach ( $err as $key => $value  ){
		if(preg_match("/^\d*$/",$key)){
			#过滤数字
		}else{
			$sql .= $key."='".addslashes($value)."', ";
		}
	}
	$sql = substr($sql, 0, strlen($sql)-2) .  " WHERE id=".$id;

	@mysql_query($sql) or die (mysql_error());

}

/*
*插入实例
*$table=表名
*$err=数组
*/
function add_insert_cl($table,$err){
	$sql = "INSERT INTO ".$table." (";
	foreach ( $err as $key => $value  ){
		$sql .= $key.", ";
	}
	$sql = substr($sql, 0, strlen($sql)-2) .  ") VALUES (";
	foreach ( $err as $key => $value  ){
		$sql .= "'".addslashes($value)."', ";
	}
	$sql = substr($sql, 0, strlen($sql)-2) .  ")";
	@mysql_query($sql) or die (mysql_error());
	return mysql_insert_id();
}
/*
$sys=getone("select * from clt_system where id=1");
$xingqi = date("w");
if($sys['jingtai']=='close'){
	echo "fuck you!";
	exit;
}
$timeday=strtotime(date("Y-m-d"));
if($timeday<=$sys['day_time']){
	echo "fuck you!";
	exit;
}



$time=unserialize($sys['jingtai_ff']);
if(in_array($xingqi, $time) || in_array(8, $time)) {
}else {
	echo "fuck you!!";
	exit;
}*/

/*修改或者新增bonus表*/
function edit_add_bonus($uid,$base_mey_per){
	/*修改bonus表,先查一下今天有没有数据*/
	$td_time = strtotime(date('Y-m-d'));
	$sql_check_bouns = "select * from clt_bonus where uid='".$uid."' and `createtime`>='".$td_time."'";
	$sql_check_bouns = getone($sql_check_bouns);
	if(!empty($sql_check_bouns['id'])){
		//如果有数据，直接修改对应的列
		$sql_bonus = "update clt_bonus set `b1`=(b1+$base_mey_per),`b0`=(b0+$base_mey_per) where id='".$sql_check_bouns['id']."'";
	}else{
		$sql_bonus = "insert into clt_bonus (is_type,uid,b0,b1,createtime) values(0,'".$uid."',$base_mey_per,$base_mey_per,'".time()."');";
	}
	querys($sql_bonus);
}

//统计每日奖金（日封顶基础）
function bonus_insert_new($b0,$uid,$b1,$b2,$b3,$b4,$b5,$b6,$b7=0,$b8=0,$b9=0,$b10=0,$b11=0,$b12=0,$b13=0) {
	$date=strtotime( date("Y-m-d"));
	$sql_bonus="select * from clt_bonus where createtime>".$date." and uid=".$uid;
	$bonus=getone($sql_bonus);
	if (!empty($bonus)){
		$bonus_update = NULL;
		$bonus_update ['b0'] = $bonus ['b0'] + $b0;
		$bonus_update ['b1'] = $bonus ['b1'] + $b1;
		$bonus_update ['b2'] = $bonus ['b2'] + $b2;
		$bonus_update ['b3'] = $bonus ['b3'] + $b3;
		$bonus_update ['b4'] = $bonus ['b4'] + $b4;//积分
		$bonus_update ['b5'] = $bonus ['b5'] + $b5;
		$bonus_update ['b6'] = $bonus ['b6'] + $b6;
		$bonus_update ['b7'] = $bonus ['b7'] + $b7;
		$bonus_update ['b8'] = $bonus ['b8'] + $b8;
		$bonus_update ['b9'] = $bonus ['b9'] + $b9;
		$bonus_update ['b10'] = $bonus ['b10'] + $b10;
		$bonus_update ['b11'] = $bonus ['b11'] + $b11;
		$bonus_update ['b12'] = $bonus ['b12'] + $b12;
		$bonus_update ['b13'] = $bonus ['b13'] + $b13;
		edit_update_cl ( 'clt_bonus', $bonus_update, $bonus ['id'] );

		/*	db('bonus')->where('id',$bonus['id'])->update($bonus_update);*/
		//edit_update_cl ( 'bonus', $bonus_update, $bonus ['id'] );
	} else {
		$bonus_update = NULL;
		$bonus_update ['createtime'] = time();
		$bonus_update ['uid'] = $uid;
		$bonus_update ['b0'] = $b0;
		$bonus_update ['b1'] = $b1;
		$bonus_update ['b2'] = $b2;
		$bonus_update ['b3'] = $b3;
		$bonus_update ['b4'] = $b4;
		$bonus_update ['b5'] = $b5;
		$bonus_update ['b6'] = $b6;
		$bonus_update ['b7'] = $b7;
		$bonus_update ['b8'] = $b8;
		$bonus_update ['b9'] = $b9;
		$bonus_update ['b10'] = $b10;
		$bonus_update ['b11'] = $b11;
		$bonus_update ['b12'] = $b12;
		$bonus_update ['b13'] = $b13;
		add_insert_cl ( 'clt_bonus', $bonus_update );
		//	db('bonus')->insert($bonus_update);
	}

}

//业绩统计
function achievement_new($a,$b,$c,$d,$e,$f,$g,$h,$i,$j=0,$k=0,$l=0,$m=0,$n=0){
	$createtime=strtotime(date('Y-m-d'));

	$achievement=getone("select * from clt_total_achievement where createtime=".$createtime." ");
	if(!empty($achievement)){
		$achievement_change['createtime']=$createtime;
		$achievement_change['newadd_achievement']=$achievement['newadd_achievement']+$a;
		$achievement_change['all_achievement']=$achievement['all_achievement']+$b;
		$achievement_change['newadd_user']=$achievement['newadd_user']+$c;
		$achievement_change['all_user']=$achievement['all_user']+$d;
		$achievement_change['new_grant']=$achievement['new_grant']+$e;
		$achievement_change['all_grant']=$achievement['all_grant']+$f;
		$achievement_change['bonus1']=$achievement['bonus1']+$g;
		$achievement_change['bonus2']=$achievement['bonus2']+$h;
		$achievement_change['bonus3']=$achievement['bonus3']+$i;
		$achievement_change['bonus4']=$achievement['bonus4']+$j;
		$achievement_change['bonus5']=$achievement['bonus5']+$k;
		$achievement_change['bonus6']=$achievement['bonus6']+$l;
		$achievement_change['bonus7']=$achievement['bonus7']+$m;
		$achievement_change['bonus8']=$achievement['bonus8']+$n;
		edit_update_cl ( 'clt_total_achievement', $achievement_change, $achievement['id'] );
		//	db('total_achievement')->where('id',$achievement['id'])->update($achievement_change);
	}else{
		$all_achievement=querys("select all_achievement from clt_total_achievement where id>0 order by id desc limit 0,1");
		$all_user=querys("select all_user from clt_total_achievement where id>0 order by id desc limit 0,1");
		$all_grant=querys("select all_grant from clt_total_achievement where id>0 order by id desc limit 0,1");
		$achievement_change['createtime']=$createtime;
		$achievement_change['newadd_achievement']=$a;
		$achievement_change['all_achievement']=$b+$all_achievement['all_achievement'];
		$achievement_change['newadd_user']=$c;
		$achievement_change['all_user']=$d+$all_user['all_user'];
		$achievement_change['new_grant']=$e;
		$achievement_change['all_grant']=$f+$all_grant['all_grant'];
		$achievement_change['bonus1']=$g;
		$achievement_change['bonus2']=$h;
		$achievement_change['bonus3']=$i;
		$achievement_change['bonus4']=$j;
		$achievement_change['bonus5']=$k;
		$achievement_change['bonus6']=$l;
		$achievement_change['bonus7']=$m;
		$achievement_change['bonus8']=$n;
		add_insert_cl ( 'clt_total_achievement', $achievement_change );
		//	db('total_achievement')->where('id',$achievement['id'])->insert($achievement_change);
	}
}
//字符串的切割
function ppathnew($ppath){
	$len=strlen($ppath);
	$str=substr($ppath,1,$len-2);//$len-16
	$arr=explode(",",$str);
	return $arr;
}
function money_log($id,$money,$beizhu,$jifen,$source,$type){
	if($type==1){
		$str="+";
		$shiji=$money+$jifen;
	}else{
		$str="-";
		$shiji=$money-$jifen;
	}
	//增加积分变动日志
	$addinfo['type']=1;
	$addinfo['userid']=$id;
	$addinfo['dangqian']=$shiji;
	$addinfo['num']=$str.$money;
	$addinfo['source']=$source;
	$addinfo['time']=time();
	if(!empty($beizhu)){
		$addinfo['beizhu']=$beizhu;

	}
	add_insert_cl ( 'clt_money_log', $addinfo );
}
//获取配置信息
function getsystemnew(){
	$sys=getone("select * from clt_system where id=1");
	return $sys;
}
function getlevel($level){
	$level=getone("select * from clt_user_level where `level`=".$level);
	return $level;
}
//日封顶
function day_caps_new($id,$money,$type){

	$ulevel=getone("select `level` from clt_users where id=".$id);
	$level=getlevel($ulevel['level']);
	$date=strtotime(date("Y-m-d"));
/*	$bonus=db('bonus')->field('b1,b2,b3,b4,b5,b6')->where('createtime','gt',$date)->where('uid',$id)->find();*/
	$bonus=getone("select b1,b2,b3,b4,b5,b6 from clt_bonus where createtime>".$date." and uid=".$id);
	if($type==1){
		$mey =$level['b5'] -$bonus['b4'];
	}else{
		$mey =$level['b9'] -$bonus['b4'];
	}
	if ($mey>0){
		if ($mey>$money) {
			return array($money,0);
		} elseif($mey==$money){
			return array($money,1);
		} else{
			return array($mey,1);
		}
	} else {
		return array(0,0);
	}
}
function jifen_new($type,$userid,$num,$source,$jiajian,$beizhu){
	$b1=0;
	$b2=0;
	$b3=0;
	$b4=0;
	$b5=0;
	$b6=0;
	$b7=0;
	if($source==9){
		$b1=$num;
	}elseif($source==10){
		$b2=$num;
	}elseif($source==11){
		$b3=$num;
	}elseif($source==12){
		$b4=$num;//积分
	}elseif($source==13){
		$b5=$num;
	}elseif($source==14){
		$b6=$num;
	}
	bonus_insert_new($num,$userid,$b1,$b2,$b3,$b4,$b5,$b6,$b7);
	$user=getone("select money_jifen from clt_users where id=".$userid);
	if($jiajian==1){
		querys("update clt_users set money_jifen=money_jifen+".$num ." where id=".$userid);
	}else{
		querys("update clt_users set money_jifen=money_jifen-".$num ." where id=".$userid);
	}
	money_log($userid,$num,$beizhu,$user['money_jifen'],$source,$jiajian);
}
function  bonusdel_new($id,$money,$mobile){
	$sys=getsystemnew();
	$sxmoney=round($money*$sys['shouxufei']/100,2);
	$guamai_fei=round($money*$sys['guamai_fei']/100,2);
	$user=getone("select money_jifen from clt_users where id=".$id);
	querys("update clt_users set money_jifen=money_jifen-".$sxmoney ." where id=".$id);
	money_log($id,$sxmoney,$mobile,$user['money_jifen'],16,2);
	$user=getone("select money_jifen from clt_users where id=".$id);
	querys("update clt_users set money_jifen=money_jifen-".$guamai_fei ." where id=".$id);
	money_log($id,$guamai_fei,$mobile,$user['money_jifen'],15,2);
/*	jifen(1,$id,$sxmoney,16,2,$mobile);
	jifen(1,$id,$guamai_fei,15,2,$mobile);*/
	achievement_new(0,0,0,0,0,0,0,0,0,0,0,0,$sxmoney);
}
//返还奖
function fanhuan($id,$mey){
	$pusers=getall("select id,`level`,mobile from clt_users where up_userid=".$id." and  activation=1 order by id desc");
/*	$pusers=db('users')
		->field('id,level')
		->where('up_userid',$id)
		->where('activation',1)
		->order('id desc')
		->select();*/
	$count=getone("select count(*) as c from clt_users where up_userid=".$id." and  activation=1 order by id desc");
/*	$count=db('users')
		->where('up_userid',$id)
		->where('activation',1)
		->order('id desc')
		->count();*/

	$user_level=getlevel(1);
	$koudian=$mey*$user_level['b6']/100;
/*	jifen(1,$id,$koudian,18,2,'返还扣点');*/
	$user=getone("select money_jifen from clt_users where id=".$id);
	querys("update clt_users set money_jifen=money_jifen-".$koudian ." where id=".$id);
	money_log($id,$koudian,"返还扣点",$user['money_jifen'],18,2);
	if(!empty($pusers)){
		foreach ($pusers as $key=>$v){
			$level=getlevel($v['level']);
			$money=round($mey/$count['c']*$level['b6']/100,2);
			if($money>0){
				$beizhu="返还奖";
	/*			$user=getone("select money_jifen from clt_users where id=".$id);*/
		/*		querys("update clt_users set money_jifen=money_jifen+".$money ." where id=".$v['id']);*/
/*
				money_log($id,$koudian,$beizhu,$user['money_jifen'],13,1);*/
				bonus_insert_new($money,$v['id'],0,0,0,0,$money,0,0);
				jifen_new(1,$v['id'],$money,13,1,$beizhu);
				bonusdel_new($v['id'],$money,$v['mobile']);
				achievement_new(0,0,0,0,$money,$money,0,0,0,0,$money);
			}
		}
	}
}


/*----------常用方法封装  end------------*/

//见点奖
function jiandian($id){
	$user=getone("select * from clt_users where id=".$id);
	$ppath=trim($user['ppath'],',');
	/*$pusers=db('users')
		->field('level,plevel,id')
		->where('id','in',$user['ppath'])
		->where('plevel','between',[0,$user['plevel']])
		->select();*/
	$pusers=getall("select * from clt_users where id in (".$ppath.") and plevel>=0 and plevel<=".$user['plevel']);
/*	echo "select * from clt_users where id in (".$ppath.") and plevel>=0 and plevel<=".$user['plevel'];
	var_dump($pusers);
	exit;*/
	$user_level=getlevel($user['level']);
	if(!empty($pusers)){
		foreach ($pusers as $key=>$v){
			//  $ulevel=$v['level']>$user['level']?$v['level']:$user['level'];
			$level=getlevel($v['level']);

			$cha=$user['plevel']-$v['plevel'];
			if($cha<=$level['b2']){
				if($v['level']==1){
					$money=$level['b1'];
				}elseif($v['level']==2){
					if($user['level']==1){
						$money=$user_level['b1'];
					}else{
						$money=$level['b1'];
					}
				}else{
					$money=$user_level['b1'];
				}

			}else{
				$money=0;
			}
			if($money>0){
				$beizhu=$user['mobile'];
				jifen_new(1,$v['id'],$money,9,1,$beizhu);
				bonusdel_new($v['id'],$money,$v['moble']);
				achievement_new(0,0,0,0,$money,$money,$money,0,0);
				fanhuan($v['id'],$money);
			}
		}
	}
}

//层碰奖
function cengpeng($id,$upid='',$xfpath=''){
	$me=getone("select * from clt_users where id=".$id);
/*	$all=db('users')
		->field('level,ppath')
		->where('plevel',$me['plevel'])
		->where('activation',1)
		->where('id','neq',$me['id'])
		->order('id asc')
		->select();*/
	$sql="select `level`,ppath from clt_users where plevel=".$me['plevel']." and activation=1 and id<>".$me['id']." order by id asc";
	$all=getall($sql);//得到激活会员同一层所有的会员
	$count=count($all);//人数
/*	$path=ppath($me['id']);*/
/*	$count=db('users')->where('plevel',$me['plevel'])
		->where('activation',1)
		->where('id','neq',$me['id'])
		->count();*/
	$path=ppathnew($me['ppath']);
	for($z=0;$z<$count;$z++){
		$arr2=ppathnew($all[$z]['ppath']);//第一个会员的关系链
		$num=count($arr2);
		$num-=1;
		for($i=$num;$i>=0;$i--){
			if($path[$i]==$arr2[$i]){

/*				$us=db('users')->field('plevel,level,ceng,yarea1,yarea2,id')->where('id',$arr2[$i])->where('activation',1)->find();*/
				$us=getone("select plevel,`level`,ceng,yarea1,yarea2,id,mobile from clt_users where id=".$arr2[$i]." and activation=1");
				$cha=$me['plevel']-$us['plevel'];
				$level=getlevel($us['level']);
				$level1=getlevel($all[$z]['level']);
				$level2=getlevel($me['level']);
				$minlsk=$level1['lsk']>$level2['lsk']?$level2['lsk']:$level1['lsk'];

				$pv=$level['b7']/100;
				$b2=$minlsk*$level['b3']/100*$pv;
				if($us['ceng']<$me['plevel']){
					if($b2>0){
						$beizhu=$cha.'拓展奖';
						achievement_new(0,0,0,0,$b2,$b2,0,0,$b2,0,0);
						jifen_new(1,$us['id'],$b2,11,1,$beizhu);
						bonusdel_new($us['id'],$b2,$us['mobile']);
/*						bonus6($us['id'],$b2);*/
						fanhuan($us['id'],$b2);
					}
					querys("update clt_users set ceng=".$me['plevel']." where id=".$us['id']);
	/*				db('users')->where('id',$us['id'])->setField('ceng',$me['plevel']);*/
				}
				break;
			}
		}
	}
}

function b3bonusd(){
	$sql=getall("select yarea1,yarea2,id,`level`,mobile from clt_users where yarea1>0 and yarea2>0 and  activation>0");
/*	$sql=db('users')
		->field('yarea1,yarea2,id,level')
		->where('yarea1','gt', 0 )
		->where('yarea2','gt', 0 )
		->where('activation','gt',0)
		->select();*/
	foreach($sql as $key=>$v){
		$_member_update=NULL;
		$y1=$v['yarea1'];
		$y2=$v['yarea2'];
		$dan=0;
		if($y1>=$y2){
			$dan=$y2;
			$y1=$y1-$y2;
			$y2=0;
		}elseif($y1<=$y2){
			$dan=$y1;
			$y2=$y2-$y1;
			$y1=0;
		}
		if ($dan>0){
			$_member_update['yarea1']=$y1;
			$_member_update['yarea2']=$y2;
			querys("update clt_users set yarea1=".$y1.",yarea2=".$y2." where id=".$v['id']);
/*			db('users')->where("id",$v['id'])->update($_member_update);*/
			$feng2=getlevel($v['level']);
			$pv=$feng2['b7']/100;
			$b3=$feng2['b4']*$dan/100*$pv;
			if($b3>0){
				$arr=day_caps_new($v['id'],$b3,1);//日封顶
				if($arr[0]>0){
					if($arr[1]==1){
						$beizhu="日封顶";
					}else{
						//就是一个备注
						$beizhu='分红奖';
					}
					jifen_new(1,$v['id'],$arr[0],12,1,$beizhu);
					achievement_new(0,0,0,0,$arr[0],$arr[0],0,0,0,$arr[0]);
					bonusdel_new($v['id'],$arr[0],$v['mobile']);
	/*				bonus6($v['id'],$arr[0]);*/
					fanhuan($v['id'],$arr[0]);
				}
			}
		}
	}
}

?>



